<?php

namespace app\api\controller;

class Index extends Basic
{

    public function index()
    {
    }

    /**
     * 公共参数设置
     */
    public function getSetting()
    {
        $get = input('get.');

        if (empty($get['type']) || !in_array($get['type'], [1, 2, 3, 4, 5])) {
            return $this->json([], 2000, '参数错误');
        }

        $where = ['type' => $get['type'], 'status' => 1];
        $info = $this->findTable('setting', ['where' => $where, 'order' => 'id desc']);

        return $this->json($info, 1000);
    }

    /**
     * 获取banner
     */
    public function getbanner()
    {
        $get = input('get.');

        $where = ['status' => 1];
        $info = $this->findTable('banner', ['where' => $where, 'order' => 'id desc']);

        $info['banners'] = empty($info['banners']) ? [] : explode(',', $info['banners']);
        return $this->json($info, 1000);
    }

    public function getMemberInfo()
    {
        $memberid = input('get.memberid');

        if (empty($memberid)) {
            return $this->json([], 2000, '参数错误');
        }

        $where = ['memberid' => $memberid];
        $info = $this->findTable('member', ['where' => $where, 'field' => 'membername,membermobile,membernickname,membertruename,memberavatar,memberbirthday,memberaddressinfo,membersex,memberdesc']);

        return $this->json($info, 1000);
    }

    public function getCoachInfo()
    {
        $get = input('get.');
        $coachid = $get['coachid'];

        if (empty($coachid)) {
            return $this->json([], 2000, '参数错误');
        }

        if (!empty($get['longitude']) && !empty($get['latitude'])) {
            $longitude = $get['longitude'];
            $latitude = $get['latitude'];
        }

        $where = ['coachid' => $coachid];
        $info = $this->findTable('coach', ['where' => $where, 'field' => 'coachname,coachmobile,coachnickname,coachtruename,coachavatar,coachbirthday,coachaddressinfo,coachsex,coachdesc,seniority,school,coachprovinceid,coachcityid,coachareaid']);

        $province = '';
        if(!empty($info['coachprovinceid'])) {
            $area = $this->findTable('area', ['where' => ['id' => $info['coachprovinceid']], 'field' => 'name']);

            if(! empty($area['name'])) {
                $province = $area['name'];
            }
        }

        $info['province'] = $province;
        $city = '';
        if(!empty($info['coachcityid'])) {
            $area = $this->findTable('area', ['where' => ['id' => $info['coachcityid']], 'field' => 'name']);

            if(! empty($area['name'])) {
                $city = $area['name'];
            }
        }

        $info['city'] = $city;
        $area = '';
        if(!empty($info['coachareaid'])) {
            $area = $this->findTable('area', ['where' => ['id' => $info['coachareaid']], 'field' => 'name']);

            if(! empty($area['name'])) {
                $area = $area['name'];
            }
        }

        $info['area'] = $area;

        if (!empty($longitude) && !empty($latitude) && !empty($info['longitude']) && !empty($info['latitude'])) {
            /*需要传入经纬度，默认正序*/
            $info['distance'] = distance($latitude, $longitude, $info['latitude'], $info['longitude']); 
        }
        return $this->json($info, 1000);
    }

    public function getEvaluateList()
    {
        $get = input('get.');

        if (empty($get['coachid'])) {
            return $this->json([], 2000, '请求参数错误');
        }

        $data = $where = [];
        if (!empty($get['begin']) && !empty($get['end'])) {
            $begin = strtotime($get['begin']);
            $end = strtotime(date('Y-m-d', strtotime('+1 day', $get['end'])));
            $where[] = ['addtime', 'between', [$begin, $end]];
        }

        if (!empty($get['scoretype']) && in_array($get['scoretype'], [1, 2, 3])) {
            $scoretype = [1 => [5], 2 => [4, 3], [2, 1]];
            $where[] = ['score', 'in', $scoretype[$get['scoretype']]];
        }

        $where[] = ['coachid', '=', $get['coachid']];
        $where[] = ['status', '=', 1];
        $where[] = ['isdel', '=', 0];
        $data['where'] = $where;
        $data['tableName'] = 'evaluate';
        $data['totalCount'] = $this->countTable('evaluate', $data);
        $data = $this->buildCount($data);
        $data['order'] = 'addtime desc';
        $list = $this->listTable('evaluate', $data);

        $member = $this->listTable('member', [
            'where' => [
                ['memberid', 'in', array_column($list, 'memberid')],
            ],
            'field' => 'memberavatar,membertruename,memberbirthday,memberid,membersex,memberage',
        ]);

        $order = $this->listTable('order', [
            'where' => [
                'orderid' => array_column($list, 'orderid'),
            ],
            'field' => 'ordername,ordersn,orderid',
        ]);

        foreach ($list as $key => $val) {
            $list[$key]['addtime'] = empty($val['addtime']) ? '' : date('Y-m-d H:i:s', $val['addtime']);
            $list[$key]['updatetime'] = empty($val['updatetime']) ? '' : date('Y-m-d H:i:s', $val['updatetime']);
            $list[$key]['imgs'] = empty($val['imgs']) ? [] : explode(',', $val['imgs']);
            foreach ($member as $v) {
                if ($v['memberid'] == $val['memberid']) {
                    $list[$key]['membertruename'] = $v['membertruename'];
                    $list[$key]['memberavatar'] = $v['memberavatar'];
                    $list[$key]['membersex'] = $v['membersex'];
                    $list[$key]['age'] = empty($v['memberbirthday']) ? (empty($v['memberage']) ? '暂无' : $v['memberage']) : birthday($v['memberbirthday']);
                }
            }
            foreach ($order as $ov) {
                if ($ov['orderid'] == $val['orderid']) {
                    $list[$key]['ordername'] = $ov['ordername'];
                    $list[$key]['ordersn'] = $ov['ordersn'];
                }
            }
        }
        $data['list'] = $list;
        return $this->json($data, 1000);
    }

    public function getPoster()
    {
        $get = input('get.');

        $data = $where = [];
        if (!empty($get['begin']) && !empty($get['end'])) {
            $begin = strtotime($get['begin']);
            $end = strtotime(date('Y-m-d', strtotime('+1 day', $get['end'])));
            $where[] = ['addtime', 'between', [$begin, $end]];
        }

        $where[] = ['status', '=', 1];
        $where[] = ['isdel', '=', 0];
        $data['where'] = $where;
        $data['tableName'] = 'poster';
        $data['totalCount'] = $this->countTable('poster', $data);
        $data = $this->buildCount($data);
        $list = $this->listTable('poster', $data);

        foreach ($list as $key => $val) {
            $list[$key]['addtime'] = empty($val['addtime']) ? '' : date('Y-m-d H:i:s', $val['addtime']);
        }
        $data['list'] = $list;
        return $this->json($data, 1000);
    }

    public function getPosterDetail()
    {
        $posterid = input('get.posterid');

        if (empty($posterid)) {
            return $this->json([], 2000, '请求参数错误');
        }

        $where = ['posterid' => $posterid];
        $info = $this->findTable('poster', ['where' => $where]);

        return $this->json($info, 1000);
    }

     /**
     * 获取banner
     */
    public function getSchool()
    {
        $get = input('get.');

        $where = ['status' => 1];
        $info = $this->listTable('school', ['where' => $where, 'order' => 'id desc', 'nolimit'=>1]);

        return $this->json($info, 1000);
    }

}
